from mohe.core.obj import obj_trade
import datetime
class open_time:
    def __init__(self,name="",obj=None,obj_trade_tool=None,start_mins=None,end_mins=None,isin=True):
        self.start_mins=start_mins
        self.end_mins=end_mins
        self.isin=isin
        if obj_trade_tool is not None:
            self.obj_trade_tool=obj_trade_tool
        else:
            self.obj_trade_tool = obj_trade(obj=obj, delta=1)
        if name=="":
            self.name = type(self).__name__
        else:
            self.name = type(self).__name__+"("+str(name)+")"
    def reset(self):
        pass
    def onbar(self,ss,b):
        if "uptime" in b.keys():
            tk=b["uptime"]
        else:
            tk=b["timekey"]
        ts=self.obj_trade_tool.gettrade_datetime(tk)
        if self.start_mins:
            if self.end_mins:
                start0=ts[0][0]+datetime.timedelta(minutes=self.start_mins)
                end0=ts[-1][-1]-datetime.timedelta(minutes=self.start_mins)
                if self.isin:
                    test1=tk>=start0 and tk<=end0
                else:
                    test1 = tk <= start0 or tk >= end0
                return test1
            else:
                start0 = ts[0][0] + datetime.timedelta(self.start_mins)
                if self.isin:
                    test1 = tk >= start0
                else:
                    test1 = tk <= start0
                return test1
        else:
            if self.end_mins:
                end0 = ts[-1][-1] - datetime.timedelta(self.start_mins)
                if self.isin:
                    test1 = tk <= end0
                else:
                    test1 = tk >= end0
                return test1
            else:
                return True